/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.admin.Citys;

/**
 *
 * @author Danh
 */
public class CityManager {

    Connection conn;

    public CityManager() {
        try {
            conn = MyConn.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Citys> getCity() {
        List<Citys> list = new ArrayList<Citys>();
        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT City_Id,City_Name,Country_Name FROM Cities join Countries on Cities.Country_Id=Countries.Country_Id");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Citys c = new Citys(rs.getNString("City_Name"), rs.getInt("City_Id"), rs.getNString("Country_Name"));
                list.add(c);

            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean deleteCity(int id) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM Advertisement_Details  WHERE City_Id=?");
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
            PreparedStatement pstmt1 = conn.prepareStatement("DELETE FROM Cities WHERE City_Id=?");
            pstmt1.setInt(1, id);
            int eu = pstmt1.executeUpdate();
            if (eu > 0) {
                return true;
            }

        } catch (Exception e) {
        }

        return false;
    }

    public boolean update(Citys c) {
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement("UPDATE Cities SET City_Name=? ,Country_Id =? WHERE City_Id=?");

            pstmt.setNString(1, c.getCity_Name());
            pstmt.setInt(2, c.getCountry_Id());
            pstmt.setInt(3, c.getCity_Id());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean insertCity(Citys c) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Cities(Country_Id,City_Name) VALUES(?,?)");
            pstmt.setInt(1, c.getCountry_Id());
            pstmt.setNString(2, c.getCity_Name());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    public static void main(String[] args) {
        CityManager mana = new CityManager();
        Citys city = new Citys(1, "wtf", 1);
        boolean update = mana.update(city);
        if (update) {
            System.out.println("thanh cong");
        }
    }
}
